﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Modules.Orders.Services;
using Autorent.Desktop.Modules.Reports;
using Autorent.Desktop.Modules.Reports.OfficesReports.Views;
using Autorent.Desktop.Modules.Reports.OfficesReports.ReportsDataSets;

namespace Autorent.Desktop.Modules.Reports.OfficesReports.ViewModels
{
    public class SummaryMonthOfficeReportViewModel
    {
        private readonly IAutoOrdersRepository _autoOrdersRepository;

        public SummaryMonthOfficeReportViewModel(SummaryMonthOfficeReportView view, IAutoOrdersRepository autoOrdersRepository)
        {
            this.View = view;
            view.Model = this;

            _autoOrdersRepository = autoOrdersRepository;

            SummaryMonthOfficeReportDataSet summaryMonthOfficeReportDataSet = new SummaryMonthOfficeReportDataSet();
            FillSummaryMonthOfficeReportDataTable(summaryMonthOfficeReportDataSet, 1);

            SummaryMonthOfficeReport report = new SummaryMonthOfficeReport();
            report.SetDataSource(summaryMonthOfficeReportDataSet);

            View.reportViewer.ViewerCore.ReportSource = report;
        }

        public SummaryMonthOfficeReportView View { get; set; }

        private void FillSummaryMonthOfficeReportDataTable(SummaryMonthOfficeReportDataSet ds, decimal OfficeID)
        {
            List<AutoOrder> autoOrders = _autoOrdersRepository.GetOrders().Where(
                o => o.Office_ID.Equals((decimal)OfficeID)).Where(
                o => o.Payment != null).ToList();

            foreach(AutoOrder autoOrder in autoOrders)
            {
                ds.SummaryMonthOfficeReportDataTable.AddSummaryMonthOfficeReportDataTableRow(
                    autoOrder.AutoOrder_ID,
                    autoOrder.Client.ClientLastName,
                    autoOrder.Client.ClientFirstName,
                    autoOrder.Client.ClientPatroname,
                    autoOrder.AutoClass.AutoClassName,
                    autoOrder.Auto.AutoModel,
                    autoOrder.Auto.AutoNumber,
                    autoOrder.Office.OfficeName,
                    (DateTime)autoOrder.AutoOrderWithdrawalDT,
                    autoOrder.Office1.OfficeName,
                    (DateTime)autoOrder.AutoOrderReturningDT,
                    autoOrder.Payment.PaymentValue
                    );
            }
        }
    }
}
